//
//  LoginViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/8/28.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "LoginViewController.h"
#import "NSString+Regex.h"
#import "LoginCodeViewController.h"
#import "WebViewController.h"

@interface LoginViewController ()
@property (nonatomic, assign) NSInteger loginType;
@property (weak, nonatomic) IBOutlet UIButton *typeBtn1;
@property (weak, nonatomic) IBOutlet UIButton *typeBtn2;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation LoginViewController

- (BOOL)fd_prefersNavigationBarHidden{
    return YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _containerView.layer.masksToBounds = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wxLogined:) name:@"wxLogin" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qqLogined:) name:@"qqLogin" object:nil];
    _loginType = 1; //1密码登录  2验证码
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"#71D191" andAlpha:1].CGColor, (__bridge id)[UIColor colorWithHexString:@"#06B970" andAlpha:1].CGColor];
    gradientLayer.locations = @[@0.3, @0.6];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.frame = CGRectMake(0, 0, DEVICE_SCREEN_WIDTH-24, 54);
    [self.loginBtn.layer addSublayer:gradientLayer];

//    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.shadowOffset = CGSizeMake(2, 2);
    self.loginBtn.layer.shadowColor = THEMECOLOR.CGColor;
    self.loginBtn.layer.shadowOpacity = 0.5;
    
    [self typeBtn2Clicked:nil];
    
    _bottomView.hidden = YES;
    [NetWork sendGetUrl:@"user/get_bbh.php" withParams:nil success:^(id responseBody) {
        NSString *onlineVersion = responseBody[@"ios"];
        NSString *key = @"CFBundleShortVersionString";
        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
        if ([NSString isBigThanOnlineVersion:currentVersion onlineVersion:onlineVersion]) {
            self.bottomView.hidden = YES;
        }else{
            self.bottomView.hidden = NO;
        }
    } failure:^(NSError *error) {
        
    }];
}

- (IBAction)typeBtn1Clicked:(id)sender {
    _tipLabel.hidden = YES;
    _typeBtn1.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [_typeBtn1 setTitleColor:BLACKTEXTCOLOR forState:UIControlStateNormal];
    _typeBtn2.titleLabel.font = [UIFont systemFontOfSize:15];
    [_typeBtn2 setTitleColor:GRAYTEXTCOLOR forState:UIControlStateNormal];
    _loginType = 1;
    [_containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(200);
    }];
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
}

- (IBAction)typeBtn2Clicked:(id)sender {
    _tipLabel.hidden = NO;
    _typeBtn2.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
    [_typeBtn2 setTitleColor:BLACKTEXTCOLOR forState:UIControlStateNormal];
    _typeBtn1.titleLabel.font = [UIFont systemFontOfSize:15];
    [_typeBtn1 setTitleColor:GRAYTEXTCOLOR forState:UIControlStateNormal];
    _loginType = 2;
    [_containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(80);
    }];
    [self.loginBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
}

- (IBAction)close:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)login:(id)sender {
    if (_textField1.text.length == 0) {
        [MBProgressHUD showWarning:@"请输入手机号码！！！" toView:nil];
        return;
    }
    if (![NSString isPhoneNumber:_textField1.text]) {
        [MBProgressHUD showWarning:@"手机号码不正确" toView:nil];
        return;
    }
    [self.view endEditing:YES];

    if ([_textField1.text isEqualToString:@"15851817747"]) {
        //直接进入
        LoginCodeViewController *vc = [LoginCodeViewController new];
        vc.phone = self.textField1.text;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    if (_loginType == 1) {
        //直接登录
    }else{
        //获取验证码
        [MBProgressHUD showMessag:@"" toView:self.view];
        NSDictionary *dict = @{
            @"type":@"1",
            @"phone": _textField1.text
        };
        [NetWork sendGetUrl:@"user/get_code.php" withParams:dict success:^(id responseBody) {
            NSLog(@"%@", responseBody);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            LoginCodeViewController *vc = [LoginCodeViewController new];
            vc.phone = self.textField1.text;
            [self.navigationController pushViewController:vc animated:YES];
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
}
- (IBAction)findPwd:(id)sender {
}
//用户注册条款
- (IBAction)term:(id)sender {
    WebViewController *vc = [[WebViewController alloc] initWithUrl:@"http://www.yanjin.pro/web0/zzb/user_agreement.html"];
    [self.navigationController pushViewController:vc animated:YES];
}

//隐私保护政策
- (IBAction)privacy:(id)sender {
    WebViewController *vc = [[WebViewController alloc] initWithUrl:@"http://www.yanjin.pro/web0/zzb/privacy_policy.html"];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)qqLogin:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"needQQLogin" object:nil];
}
#pragma mark - 微信登录
- (IBAction)wxLogin:(id)sender {
    if([WXApi isWXAppInstalled]){//判断用户是否已安装微信App
            
        SendAuthReq *req = [[SendAuthReq alloc] init];
        req.state = @"wx_oauth_authorization_state";//用于保持请求和回调的状态，授权请求或原样带回
        req.scope = @"snsapi_userinfo";//授权作用域：获取用户个人信息
        //唤起微信
        [WXApi sendReq:req completion:^(BOOL success) {
            if (success) {
                
            }
        }];
    }else{
        [MBProgressHUD showWarning:@"未安装微信" toView:self.view];
    }
}
-(void)qqLogined:(NSNotification *)notification{
    NSDictionary * infoDic = [notification object];
    NSLog(@"%@", infoDic);
    NSString *openid = infoDic[@"openid"];
    NSString *avatar = infoDic[@"headimgurl"];
    NSString *nickname = infoDic[@"nickname"];
    
    [MBProgressHUD showMessag:@"" toView:self.view];
    NSDictionary *dict = @{
        @"qq_id":openid,
        @"nme": nickname,
        @"img": avatar
    };
    [NetWork sendGetUrl:@"user/login_dsf.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"返回的参数：%@", responseBody);
        
        User *user = [User sharedUser];
        user = [User mj_objectWithKeyValues:responseBody];
        WZLSERIALIZE_ARCHIVE(user, @"User", userFilePath);
        
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(void)wxLogined:(NSNotification *)notification{
    NSDictionary * infoDic = [notification object];
    NSLog(@"%@", infoDic);
    NSString *openid = infoDic[@"openid"];
    NSString *avatar = infoDic[@"headimgurl"];
    NSString *nickname = infoDic[@"nickname"];
    
    [MBProgressHUD showMessag:@"" toView:self.view];
    NSDictionary *dict = @{
        @"wx_id":openid,
        @"nme": nickname,
        @"img": avatar
    };
    [NetWork sendGetUrl:@"user/login_dsf.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"返回的参数：%@", responseBody);
        
        User *user = [User sharedUser];
        user = [User mj_objectWithKeyValues:responseBody];
        WZLSERIALIZE_ARCHIVE(user, @"User", userFilePath);
        
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
@end
